package com.yz.house.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yz.house.R;
import com.yz.house.base.BaseFragment;
import com.yz.house.func.ListDecoration;
import com.yz.house.model.HouseHelpBean;
import com.yz.house.ui.activity.ContentDetailActivity;
import com.yz.house.ui.adaper.HouseHelpAdapter;
import com.yz.house.ui.utils.SizeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author zhangyf
 * @date 2018/4/12 0012
 */
public class HouseFragment extends BaseFragment implements OnTabSelectListener {

    @BindView(R.id.rv_help)
    RecyclerView rvHelp;
    @BindView(R.id.tl)
    SlidingTabLayout tl;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.ll_search)
    LinearLayout ll_search;
    @BindView(R.id.tv_hi)
    TextView tv_hi;
    @BindView(R.id.app_bar)
    AppBarLayout app_bar;
    @BindView(R.id.iv_total)
    ImageView iv_total;
    @BindView(R.id.iv_topic)
    ImageView iv_topic;
    @BindView(R.id.iv_activity)
    ImageView iv_activity;
    HouseHelpAdapter helpAdapter;
    private List<HouseHelpBean> helpList = Arrays.asList(new HouseHelpBean(R.mipmap.img_zkzn), new HouseHelpBean(R.mipmap.img_jjdt),
            new HouseHelpBean(R.mipmap.img_cw), new HouseHelpBean(R.mipmap.img_diy), new HouseHelpBean(R.mipmap.img_hwzc)
            , new HouseHelpBean(R.mipmap.img_jjdj), new HouseHelpBean(R.mipmap.img_lzch),
            new HouseHelpBean(R.mipmap.img_sndf), new HouseHelpBean(R.mipmap.img_zsbj)
            , new HouseHelpBean(R.mipmap.img_xss));

    private ArrayList<BaseFragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "推荐", "客厅", "卧室"
            , "厨房", "卫生间", "餐厅"
    };
    private ContentPagerAdapter mAdapter;

    public static HouseFragment newInstance(Bundle args) {
        HouseFragment fragment = new HouseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void onInitLazyData() {

    }

    @Override
    protected void onInitFastData() {

    }

    @Override
    protected void onInitFastView() {
        for (String title : mTitles) {
            mFragments.add(ContentListFragment.newInstance(title));
        }
        mAdapter = new ContentPagerAdapter(getChildFragmentManager());
        view_pager.setAdapter(mAdapter);
        tl.setViewPager(view_pager);
        tl.setOnTabSelectListener(this);
        helpAdapter = new HouseHelpAdapter(helpList);
        rvHelp.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
        rvHelp.addItemDecoration(new ListDecoration(SizeUtils.dp2px(12)));
        rvHelp.setAdapter(helpAdapter);

        app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                float ratio = Float.valueOf(Math.abs(verticalOffset)) / Float.valueOf(appBarLayout.getTotalScrollRange());
                ll_search.setAlpha(ratio);
                tv_hi.setAlpha(1 - ratio);
            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_house;
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @OnClick({R.id.iv_total, R.id.iv_topic, R.id.iv_activity})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_total:
                mActivity.readyGo(ContentDetailActivity.class);
                break;
            case R.id.iv_topic:
                mActivity.readyGo(ContentDetailActivity.class);
                break;
            case R.id.iv_activity:
                mActivity.readyGo(ContentDetailActivity.class);
                break;
        }
    }

    private class ContentPagerAdapter extends FragmentPagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
